<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <span id="title">密码修改</span>
        <el-divider></el-divider>
      </el-row>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm container">
        <el-form-item label="用户名">
          <el-input v-model="user.uname" disabled></el-input>
        </el-form-item>
        <el-form-item label="旧密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    var validateOldPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        let data = {
          account: this.user.uname,
          password: this.ruleForm.oldPass
        }
        this.$axios.post('/user/checkPass',data).then(res=>{
          if (res.data.code == 200) {
            if (res.data.data) {
              callback();
            } else {
              callback(new Error('请输入正确的密码'));
            }
          }
        })
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      user:{},
      ruleForm: {
        pass: '',
        checkPass: '',
        oldPass:''
      },
      rules: {
        pass: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        oldPass: [
          { validator: validateOldPass, trigger: 'blur'}
        ]
      }
    }
  },
  mounted(){
    if (localStorage.getItem("user")) {
      this.user = JSON.parse(localStorage.getItem("user"))
    }
  },
  methods:{
    submitForm(formName) {
      let data = {
          account: this.user.uname,
          password: this.ruleForm.pass
        }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post('/user/updatePassword',data).then(res=>{
            if (res.data.code == 200) {
              this.$message({
                message: '密码修改成功',
                type: 'success'
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.box-card {
  margin-left:5vh;
  margin-right:2vh;
  margin-top: 3vh;
  height: 88vh;
  display: flex;          /* 使元素成为 Flex 容器 */
  justify-content: center; /* 水平居中 */
  align-items: center;     /* 垂直居中 */
}
.el-input{
  width: 50vh;
}
#title{
  text-align: center;
  display: block;
  font-size: 6vh;
}
</style>